//index.js
let app = getApp()
Page({
    data: {
        imgs: [
            { url: '../image/banner-1.png' },
            { url: '../image/banner-2.png' },
            { url: '../image/banner-1.png' }
        ],
        currentSwiper: 0,
        autoplay: true,
        interval: 3000,
        showView: false,
        state: false,
        description: '', // 公司简介
        articleList: [], // 主营产品 文章列表
        copyright: [], // 版权信息
        link: []
    },
    // 轮播图指示点
    swiperChange: function(e) {
        this.setData({
            currentSwiper: e.detail.current
        })
    },
    // 页面跳转
    skip: function(event) {
      let index = event.currentTarget.dataset['index']
      if (this.data.imgs[index].type === 1) {
        wx.redirectTo({
          url: this.data.imgs[index].val
        })
      } else if (this.data.imgs[index].type === 2) {
        wx.navigateTo({
            url: '../article/article?id=' + this.data.imgs[index].val
        })
      }
    },
    onLoad: function(options) {
        // 生命周期函数--监听页面加载
        showView: (options.showView == "true" ? true : false)
        this.getAdvertList() // 获取广告位列表
        this.getArticleList() // 获取文章
        this.getLinkList() // 获取友情链接
        this.getSystemSetting() // 获取系统设置 公司简介
    },
    getAdvertList() {
        app.api.advertList({ code: 'tpl1_slider' }, res => {
            if (res.status) {
                this.setData({
                    imgs: res.data.list
                })
            }
        })
    },
    getArticleList () {
      app.api.articleList({codes: 'product'}, res => {
        if (res.status) {
          this.setData({
            articleList: res.data.list
          })
        }
      })
    },
    getLinkList () {
      app.api.linkList({}, res => {
        if (res.status) {
          this.setData({
            link: res.data
          })
        }
      })
    },
    getSystemSetting () {
      app.api.systemSetting({}, res => {
        if (res.status && res.data.website_description) {
          this.setData({
            description: res.data.website_description
          })
        }
        if (res.status && res.data.website_copyright) {
          this.setData({
            copyright: res.data.website_copyright.split('|')
          })
        }
      })
    },
    // 简介显示隐藏
    synopsisMore: function() {
        var list_state = this.data.state,
            first_state = this.data.showView;
        if (!first_state) {
            this.setData({
                showView: true
            });
        } else {
            this.setData({
                showView: false
            });
        }
        if (list_state) {
            this.setData({
                state: true
            });
        } else {
            this.setData({
                state: true
            });
        }
    },
    // 分享小程序
    onShareAppMessage: function() {
        return {
            title: '专业的移动电子商务解决方案服务商',
            path: '/pages/index/index',
            imageUrl: '/pages/image/logo.png'
        }
    },
    // 查看产品文章详情
    showArticleHandler (event) {
      let index = event.currentTarget.dataset['index']
      wx.navigateTo({
        url: '../article/article?id=' + this.data.articleList[index].id,
      })
    },
    // 点击友情链接跳转
    showLinkHandler (event) {
      let index = event.currentTarget.dataset['index']
      wx.navigateTo({
        url: this.data.link[index].url
      })
    }
})